package com.yksj.healthtalk.wallet;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ToggleButton;

import com.alibaba.fastjson.JSONObject;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog;
import com.yksj.healthtalk.comm.DoubleBtnFragmentDialog.OnDilaogClickListener;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog;
import com.yksj.healthtalk.comm.SingleBtnFragmentDialog.OnClickSureBtnListener;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.JsonsfHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.setting.SettingPhoneBound;

/**
 * 钱包安全设置
 * @author Administrator
 *
 */
public class MyWalletSettingActivity extends BaseFragmentActivity implements OnClickListener, OnDilaogClickListener, OnClickSureBtnListener{
	
	private boolean isPayPwd;
	private String isBDPhoneNum;
	private boolean isBDPhone;
	private ToggleButton mToggleButton;//余额短信提醒
	private LinearLayout mBindPhone;//绑定手机
	private boolean isNoticeSms;//是否绑定消息提醒
	private ImageView mPhoneIcon;

	@Override
	protected void onCreate(Bundle arg0) {
		// TODO Auto-generated method stub
		super.onCreate(arg0);
		setContentView(R.layout.mywallet_setting_layout);
		initWidget();
		initDate();
	}

	private void initDate() {
//		this.isPayPwd = isPayPwd;
//		this.isBDPhone = isBDPhone;
//		this.isBDPhoneNum = isBDPhoneNum;
		isPayPwd = getIntent().getExtras().getBoolean("isPayPwd");
		isBDPhone = getIntent().getExtras().getBoolean("isBDPhone");
		isBDPhoneNum = getIntent().getExtras().getString("isBDPhoneNum");
		isNoticeSms = getIntent().getExtras().getBoolean("isNoticeSms");
		if (isNoticeSms == true) {
			mToggleButton.setChecked(true);
		}
		if (isBDPhone) {
			mPhoneIcon.setVisibility(View.VISIBLE);
		}
		mToggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
				// TODO Auto-generated method stub
						if (arg1) {
							if (!isBDPhone) {
								mToggleButton.setChecked(false);
								SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "请先绑定手机号码",  MyWalletSettingActivity.this);
							}else{
								RequestParams params = new RequestParams();
								params.put("CUSTOMERID", SmartFoxClient.getLoginUserId());
								params.put("TYPE", "NOTICE");
								params.put("NOTICE_FLAG", "1");
								HttpRestClient.doHttpWalletSetting(params, new AsyncHander(1));
							}
						}else {
							RequestParams params = new RequestParams();
							params.put("CUSTOMERID", SmartFoxClient.getLoginUserId());
							params.put("TYPE", "NOTICE");
							params.put("NOTICE_FLAG", "0");
							HttpRestClient.doHttpWalletSetting(params, new AsyncHander(0));
						}
			}
		});
	}

	private void initWidget() {
		// TODO Auto-generated method stub
		initTitle();
		titleTextV.setText("钱包安全设置");
		mBindPhone = (LinearLayout) findViewById(R.id.bind_phone);
		mToggleButton = (ToggleButton)findViewById(R.id.sms);
		mPhoneIcon = (ImageView)findViewById(R.id.phone_icon);
		titleLeftBtn.setOnClickListener(this);
		mBindPhone.setOnClickListener(this);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		case R.id.bind_phone:
			Intent intent = new  Intent(this, SettingPhoneBound.class);
			startActivityForResult(intent, 2);
			break;
		default:
			break;
		}
	}
	
	
	@Override
	public void onBackPressed() {
		Intent intent = getIntent();
		intent.putExtra("isNoticeSms", isNoticeSms);
		setResult(RESULT_OK, intent);
		finish();
	}
	
	
	/***
	 * 支付密码
	 * @param v
	 */
	public void onClickPassWord(View v){
		if (!isBDPhone) {
			DoubleBtnFragmentDialog.showDefault(getSupportFragmentManager(),  "使用钱包支付,需绑定手机并设置支付密码,您目前未绑定手机.", "稍后再说", "现在绑定", MyWalletSettingActivity.this);
		}else {
			Intent intent = new Intent(getApplicationContext(), PwdSettingActivity.class);
			intent.putExtra("isPayPwd", isPayPwd);
			intent.putExtra("isBDPhoneNum", isBDPhoneNum);
			startActivityForResult(intent, 1);
		}
		
	}
	

	@Override
	public void onDismiss(DialogFragment fragment) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onClick(DialogFragment fragment, View v) {
		Intent intent = new  Intent(this, SettingPhoneBound.class);
		startActivityForResult(intent, 2);
	}
	
	class AsyncHander extends JsonsfHttpResponseHandler{
		
		private int i;
		public AsyncHander(int i) {
			super(MyWalletSettingActivity.this);
			this.i = i;
		}
		
		@Override
		public void onSuccess(int statusCode, JSONObject response) {
			if (response.containsKey("CODE")) {
				if (response.getIntValue("CODE") == 0) {
					switch (i) {
					case 0:
						isNoticeSms = false;
						break;
					case 1:
						isNoticeSms = true;
						break;
			
					default:
						break;
					}
				}else {
					SingleBtnFragmentDialog.showDefault(getSupportFragmentManager(), response.getString("INFO"));
				}
			}
			super.onSuccess(statusCode, response);
		}
	}
	
	@Override
	protected void onActivityResult(int arg0, int arg1, Intent arg2) {
		if (arg0 == 1 && arg1 == RESULT_OK) {
			isPayPwd = true;
		}else if(arg0 == 2 && arg1 == RESULT_OK){
			if (arg2.hasExtra("phone_num")) {
				isBDPhone = true;
				isBDPhoneNum = arg2.getExtras().getString("phone_num");
				mPhoneIcon.setVisibility(View.VISIBLE);
			}else {
				isBDPhone = false;
				mPhoneIcon.setVisibility(View.GONE);
				mToggleButton.setChecked(false);
			}
		}
		super.onActivityResult(arg0, arg1, arg2);
	}

	@Override
	public void onClickSureHander() {
		// TODO Auto-generated method stub
		Intent intent = new  Intent(this, SettingPhoneBound.class);
		startActivityForResult(intent, 2);
	}

}
